package com.example.lin.zyfypt_327ljd.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lin.zyfypt_327ljd.R;
import com.example.lin.zyfypt_327ljd.activity.CollectActivity;
import com.example.lin.zyfypt_327ljd.activity.FocusActivity;
import com.example.lin.zyfypt_327ljd.activity.LoginActivity;

import com.example.lin.zyfypt_327ljd.activity.MainActivity;
import com.example.lin.zyfypt_327ljd.bean.LoginBean;
import com.example.lin.zyfypt_327ljd.iface.LogoutListener;
import com.example.lin.zyfypt_327ljd.model.LogoutModel;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment5 extends Fragment implements View.OnClickListener {
    private Button gz,sc,tc;
    private TextView tvinfo;
    private View view=null;
    private static Context context;
    private SharedPreferences sp;
    private String sessionid="",username="";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        view = inflater.inflate(R.layout.fragment5, container, false);
        Toast.makeText(context, "加载Fragment我的", Toast.LENGTH_SHORT).show();
        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();
        init();
        return view;
    }
    private LogoutListener logoutListener=new LogoutListener() {
        @Override
        public void onResponse(LoginBean loginBean) {}
        @Override
        public void onFail(String msg) {System.out.println("fail");}
    };

    private void readSP() {
        sessionid=sp.getString("sessionid",null);
        username=sp.getString("username",null);
    }

    private void init() {
        tvinfo=(TextView)view.findViewById(R.id.textView_u);
        gz=(Button)view.findViewById(R.id.button_gz);
        sc=(Button)view.findViewById(R.id.button_sc);
        tc=(Button)view.findViewById(R.id.button_tc);

        tvinfo.setText(username);

        gz.setOnClickListener(this);
        sc.setOnClickListener(this);
        tc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
        case R.id.button_gz:
        System.out.println("我的关注");
            Intent intent0=new Intent(Fragment5.context,FocusActivity.class);
            startActivity(intent0);
        break;
        case R.id.button_sc:
        System.out.println("我的收藏");
            Intent intent1=new Intent(getActivity(),CollectActivity.class);
            startActivity(intent1);
            break;
            case R.id.button_tc:
                System.out.println("退出登录");
                LogoutModel logoutModel=new LogoutModel();
                logoutModel.getResultList(sessionid,logoutListener);
                Intent intent2=new Intent(Fragment5.context,LoginActivity.class);
                startActivity(intent2);
                break;
    }
    }
}
